package com.foreverwin.mes.lims.controller;

import com.foreverwin.mes.core.base.FrontPage;
import com.foreverwin.mes.core.base.AjaxResult;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.foreverwin.mes.lims.service.CommandSendService;
import com.foreverwin.mes.lims.model.CommandSend;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 *
 * @author Max
 * @since 2020-02-27
 */
@Controller
@RequestMapping("/commandSends")
public class CommandSendController {
    private final Logger logger = LoggerFactory.getLogger(CommandSendController.class);

    @Autowired
    public CommandSendService commandSendService;

    /**
    * 根据id查询
    *
    * @param id 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public AjaxResult getCommandSendById(@PathVariable String id) {
        CommandSend result;
        try {
            result = commandSendService.selectById(id);
        } catch (Exception e) {
            logger.error("getCommandSendById -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public AjaxResult getCommandSendList(CommandSend commandSend){
        List<CommandSend> result;
        try {
            EntityWrapper<CommandSend> EntityWrapper = new EntityWrapper<>();
            EntityWrapper.setEntity(commandSend);
            result = commandSendService.selectList(EntityWrapper);
        } catch (Exception e) {
            logger.error("getCommandSendList -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/getCommandSendPageList")
    public AjaxResult getCommandSendPageList(FrontPage<CommandSend> frontPage, CommandSend commandSend){
        Page result;
        try {
            String site = CommonMethods.getSite();
            commandSend.setSite(site);
            EntityWrapper<CommandSend> EntityWrapper = new EntityWrapper<>();
            EntityWrapper.setEntity(commandSend);
            if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
                //TODO modify global query
                EntityWrapper
                    .like("handle", frontPage.getGlobalQuery())
                    .or().like("site", frontPage.getGlobalQuery())
                    .or().like("messageType", frontPage.getGlobalQuery())
                    .or().like("reqId", frontPage.getGlobalQuery())
                    .or().like("factory", frontPage.getGlobalQuery())
                    .or().like("lineId", frontPage.getGlobalQuery())
                    .or().like("operation", frontPage.getGlobalQuery())
                    .or().like("station", frontPage.getGlobalQuery())
                    .or().like("resrce", frontPage.getGlobalQuery())
                    .or().like("resrceDesc", frontPage.getGlobalQuery())
                    .or().like("sendTime", frontPage.getGlobalQuery())
                    .or().like("mode", frontPage.getGlobalQuery())
                    .or().like("code", frontPage.getGlobalQuery())
                    .or().like("message", frontPage.getGlobalQuery())
                    .or().like("userId", frontPage.getGlobalQuery())
                    .or().like("result", frontPage.getGlobalQuery())
                    .or().like("creator", frontPage.getGlobalQuery())
                    .or().like("updater", frontPage.getGlobalQuery())
        ;
            }
            result = commandSendService.selectPage(frontPage.getPagePlus(), EntityWrapper);
        } catch (Exception e) {
            logger.error("getCommandSendPageList -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 下达停复机指令
     * @param commandSend  传递的实体
     * @return  null 失败  实体成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_save")
    public AjaxResult commandSendSave(@RequestBody CommandSend commandSend) {
        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();
        int count;
        try {
            count = commandSendService.saveAndSendCommand(commandSend);
        } catch (Exception e) {
            logger.error("commandSendSave -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(count == 0 ? null : commandSend);
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id:.+}")
    public AjaxResult commandSendDelete(@PathVariable("id") String id){
        int count;
        try {
            count = commandSendService.deleteById(id) ? 1 : 0;
        } catch (Exception e) {
            logger.error("commandSendDelete -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success("count: " + count);
    }

    /**
     * 批量删除对象
     * @param ids 实体集合ID
     * @return  0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_batchDelete")
    public AjaxResult removeByIds(List<String> ids){
        int count;
        try {
            count = commandSendService.deleteBatchIds(ids) ? 1 : 0;
        } catch (Exception e) {
            logger.error("commandSendBatchDelete -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success("count: " + count);
    }
}